{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像编辑提示词重写工具测试\n",
    "\n",
    "这个笔记本用于测试 `edit_prompt_utils_openai.py` 中的图像编辑提示词重写功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 环境设置\n",
    "\n",
    "首先设置必要的环境变量和导入模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ 已从 .env 文件加载环境变量\n",
      "✅ OPENAI_API_KEY 已设置\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# 添加项目路径\n",
    "sys.path.append('src/examples/tools')\n",
    "\n",
    "# 尝试从 .env 文件加载环境变量\n",
    "def load_env_file():\n",
    "    env_file = Path('.env')\n",
    "    if env_file.exists():\n",
    "        with open(env_file, 'r', encoding='utf-8') as f:\n",
    "            for line in f:\n",
    "                line = line.strip()\n",
    "                if line and not line.startswith('#') and '=' in line:\n",
    "                    key, value = line.split('=', 1)\n",
    "                    key = key.strip()\n",
    "                    value = value.strip().strip('\"').strip(\"'\")\n",
    "                    os.environ[key] = value\n",
    "        print(\"✅ 已从 .env 文件加载环境变量\")\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "# 加载环境变量\n",
    "env_loaded = load_env_file()\n",
    "\n",
    "# 检查API密钥是否设置\n",
    "if 'OPENAI_API_KEY' not in os.environ:\n",
    "    print(\"⚠️ 未找到 OPENAI_API_KEY\")\n",
    "    if not env_loaded:\n",
    "        print(\"💡 请在项目根目录创建 .env 文件，内容如下:\")\n",
    "        print(\"   OPENAI_API_KEY=your-api-key-here\")\n",
    "    else:\n",
    "        print(\"💡 请在 .env 文件中添加 OPENAI_API_KEY=your-api-key-here\")\n",
    "    print(\"   或者设置系统环境变量 OPENAI_API_KEY\")\n",
    "else:\n",
    "    print(\"✅ OPENAI_API_KEY 已设置\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ 成功导入 edit_prompt_utils_openai 模块\n"
     ]
    }
   ],
   "source": [
    "# 导入图像编辑提示词重写工具\n",
    "try:\n",
    "    from edit_prompt_utils_openai import rewrite_edit_instruction, get_caption_language, api\n",
    "    print(\"✅ 成功导入 edit_prompt_utils_openai 模块\")\n",
    "except ImportError as e:\n",
    "    print(f\"❌ 导入失败: {e}\")\n",
    "    print(\"请确保已安装 openai 库: uv add openai\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 测试语言检测功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语言检测测试:\n",
      "'添加一只猫' -> zh\n",
      "'Add a cat' -> en\n",
      "'将背景改为蓝色' -> zh\n",
      "'Change background to blue' -> en\n",
      "'替换文字为'Hello'' -> zh\n"
     ]
    }
   ],
   "source": [
    "# 测试语言检测\n",
    "test_prompts = [\n",
    "    \"添加一只猫\",\n",
    "    \"Add a cat\",\n",
    "    \"将背景改为蓝色\",\n",
    "    \"Change background to blue\",\n",
    "    \"替换文字为'Hello'\"\n",
    "]\n",
    "\n",
    "print(\"语言检测测试:\")\n",
    "for prompt in test_prompts:\n",
    "    lang = get_caption_language(prompt)\n",
    "    print(f\"'{prompt}' -> {lang}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 测试中文编辑指令重写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中文编辑指令重写测试:\n",
      "============================================================\n",
      "\n",
      "1. 原始指令: 添加一只猫\n",
      "   重写结果: 在右下角添加一只坐着的橘猫，看向镜头\n",
      "------------------------------------------------------------\n",
      "\n",
      "2. 原始指令: 在图片中加个人\n",
      "   重写结果: 在图片中央添加一名年轻女性，全身，站姿，面向镜头。\n",
      "------------------------------------------------------------\n",
      "\n",
      "3. 原始指令: 删除背景中的车\n",
      "   重写结果: 移除背景中的汽车，并用周围环境无缝填充。\n",
      "------------------------------------------------------------\n",
      "\n",
      "4. 原始指令: 把狗换成猫\n",
      "   重写结果: 将狗替换为一只橘色虎斑猫。\n",
      "------------------------------------------------------------\n",
      "\n",
      "5. 原始指令: 将文字改为'欢迎光临'\n",
      "   重写结果: 将文字边界框替换为\"欢迎光临\"\n",
      "------------------------------------------------------------\n",
      "\n",
      "6. 原始指令: 把背景改成蓝色\n",
      "   重写结果: 将背景替换为纯蓝色背景。\n",
      "------------------------------------------------------------\n",
      "\n",
      "7. 原始指令: 转换为卡通风格\n",
      "   重写结果: 卡通风格：加粗轮廓，色彩鲜艳，简化细节，平坦阴影。\n",
      "------------------------------------------------------------\n",
      "\n",
      "8. 原始指令: 修复老照片\n",
      "   重写结果: 修复老照片，去除划痕，降低噪声，增强细节，高分辨率，真实感，自然肤色，清晰面部特征，无失真，复古照片修复\n",
      "------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 中文编辑指令测试用例\n",
    "chinese_edit_prompts = [\n",
    "    \"添加一只猫\",\n",
    "    \"在图片中加个人\",\n",
    "    \"删除背景中的车\",\n",
    "    \"把狗换成猫\",\n",
    "    \"将文字改为'欢迎光临'\",\n",
    "    \"把背景改成蓝色\",\n",
    "    \"转换为卡通风格\",\n",
    "    \"修复老照片\"\n",
    "]\n",
    "\n",
    "print(\"中文编辑指令重写测试:\")\n",
    "print(\"=\" * 60)\n",
    "\n",
    "for i, prompt in enumerate(chinese_edit_prompts, 1):\n",
    "    print(f\"\\n{i}. 原始指令: {prompt}\")\n",
    "    try:\n",
    "        rewritten = rewrite_edit_instruction(prompt)\n",
    "        print(f\"   重写结果: {rewritten}\")\n",
    "    except Exception as e:\n",
    "        print(f\"   ❌ 重写失败: {e}\")\n",
    "    print(\"-\" * 60)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 测试英文编辑指令重写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 英文编辑指令测试用例\n",
    "english_edit_prompts = [\n",
    "    \"add a cat\",\n",
    "    \"add some flowers\",\n",
    "    \"remove the car\",\n",
    "    \"replace dog with cat\",\n",
    "    \"change text to 'Hello World'\",\n",
    "    \"make background blue\",\n",
    "    \"convert to cartoon style\",\n",
    "    \"restore old photo\"\n",
    "]\n",
    "\n",
    "print(\"英文编辑指令重写测试:\")\n",
    "print(\"=\" * 60)\n",
    "\n",
    "for i, prompt in enumerate(english_edit_prompts, 1):\n",
    "    print(f\"\\n{i}. 原始指令: {prompt}\")\n",
    "    try:\n",
    "        rewritten = rewrite_edit_instruction(prompt)\n",
    "        print(f\"   重写结果: {rewritten}\")\n",
    "    except Exception as e:\n",
    "        print(f\"   ❌ 重写失败: {e}\")\n",
    "    print(\"-\" * 60)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Qwen-Image (3.12.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
